var ws=require('nodejs-websocket')

module.exports.listen = () =>{

var server=ws.createServer(function (conn) {

  let count=0
    console.log('新的连接')
    count++
    conn.username = `用户${count}`
    broadcast(`${conn.username}进入了聊天室`)
    conn.on('text', data => {
      broadcast(data)
    })

    conn.on('close', data => {
      console.log('关闭连接')
      count--
    })

    conn.on('error', data => {
      console.log('发送异常')
    })


  function broadcast(msg){
    server.connections.forEach(item=>{
      item.send(msg.toString())
    })
  }


}).listen(5201)
}
